package com.dkd.manage.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.dkd.manage.domain.dto.CancelTaskDto;
import com.dkd.manage.domain.dto.TaskDto;
import com.dkd.manage.domain.vo.TaskCountVo;
import com.dkd.manage.domain.vo.TaskVo;
import com.dkd.manage.domain.vo.TaskWorkLoadVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manage.domain.Task;
import com.dkd.manage.service.ITaskService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;

/**
 * 工单相关接口
 * 
 * @author fuyuncheng
 * @date 2025-05-11
 */
@Api(value = "/manage/task", tags = {"工单相关接口"})
@RestController
@RequestMapping("/manage/task")
public class TaskController extends BaseController
{
    @Autowired
    private ITaskService taskService;

    /**
     * 查询工单列表
     */
    @ApiOperation(value = "查询工单列表", notes = "查询工单列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('manage:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(Task task) throws ParseException {
        startPage();
        List<TaskVo> list = taskService.selectTaskListVo(task);
        return getDataTable(list);
    }
    /**
     * 根据设备编号查询补货工单和维修工单完成次数
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "innerCode", value = "", required = true)
    })
    @ApiOperation(value = "根据设备编号查询补货工单和维修工单完成次数", notes = "根据设备编号查询补货工单和维修工单完成次数", httpMethod = "GET")
    @GetMapping("/getTaskCount/{innerCode}")
    public AjaxResult getTaskCount(@PathVariable("innerCode") String innerCode) {
        TaskCountVo taskCountVo =taskService.getTaskCount(innerCode);
        return success(taskCountVo);
    }
    /**
     * 导出工单列表
     */
    @ApiOperation(value = "导出工单列表", notes = "导出工单列表", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('manage:task:export')")
    @Log(title = "工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Task task)
    {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        util.exportExcel(response, list, "工单数据");
    }

    /**
     * 获取工单详细信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "taskId", value = "", required = true)
    })
    @ApiOperation(value = "获取工单详细信息", notes = "获取工单详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('manage:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId)
    {
        return success(taskService.selectTaskByTaskId(taskId));
    }

    /**
     * 新增工单
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "TaskDto", name = "taskDto", value = "", required = true)
    })
    @ApiOperation(value = "新增工单", notes = "新增工单", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('manage:task:add')")
    @Log(title = "工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskDto taskDto)
    {
        taskDto.setAssignorId(getUserId());
        return toAjax(taskService.insertTask(taskDto));
    }

    /**
     * 修改工单
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Task", name = "task", value = "", required = true)
    })
    @ApiOperation(value = "修改工单", notes = "修改工单", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('manage:task:edit')")
    @Log(title = "工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Task task)
    {
        return toAjax(taskService.updateTask(task));
    }

    /**
     * 删除工单
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Long[]", name = "taskIds", value = "", required = true)
    })
    @ApiOperation(value = "删除工单", notes = "删除工单", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('manage:task:remove')")
    @Log(title = "工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return toAjax(taskService.deleteTaskByTaskIds(taskIds));
    }

    /**
     * 取消工单
     * @param cancelTaskDto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "CancelTaskDto", name = "cancelTaskDto", value = "", required = true)
    })
    @ApiOperation(value = "取消工单", notes = "取消工单", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('manage:task:edit')")
    @Log(title = "工单", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody CancelTaskDto cancelTaskDto)
    {
        return toAjax(taskService.CancelTask(cancelTaskDto));
    }
}
